package config
import (

	"fmt"
	"os"
	"bufio"
	"strings"
	"strconv"
)

type Config struct  {

}

//返回配置文件的map
func (r *Config) String(key string)  (val string) {
	inputFile, inputError := os.Open("application.properties")
	defer inputFile.Close()		//2016-2-14 15:52
	if inputError != nil {
		fmt.Fprintf(os.Stderr, "File Error: %s\n", inputError)
	}
	configMap:=map[string]string{}
	bufferedReader := bufio.NewReader(inputFile)
	for line , _ , err :=bufferedReader.ReadLine(); err==nil; line , _ , err =bufferedReader.ReadLine(){
		//"#"开头代表注释
		lineStr:=string(line)
		lineStr=strings.Trim(lineStr," ")
		if len(strings.Split(lineStr, "#")) > 1 {
			continue
		}
		strArr := strings.Split(lineStr,"=");
		if len(strArr) <2 {
			continue
		}
		for i := 2; i < len(strArr); i++ {
			strArr[1]+=("="+strArr[i])
		}
		strings.Trim(strArr[0]," ")
		strings.Trim(strArr[1]," ")
		configMap[strArr[0]]=strArr[1]
	}
	val=configMap[key]
	return
}

func (r *Config) Int32(key string) (int ){
	i,_:= strconv.Atoi(r.String(key))
	return i
}

func (r *Config) Float(key string) float64 {
	i,_:=strconv.ParseFloat(r.String(key),64)
	return i
}

/*
func Resource(file string)  {
	inputFile, inputError := os.Open("application.properties")
	if inputError != nil {
		fmt.Fprintf(os.Stderr, "File Error: %s\n", inputError)
	}
	bufferedReader := bufio.NewReader(inputFile)
	configMap:=map[string]string{}
	for line , _ , err :=bufferedReader.ReadLine(); err==nil; line , _ , err =bufferedReader.ReadLine(){
		//"#"开头代表注释
		if len(strings.Split(string(line), "#")) > 1 {
			continue
		}
		str := strings.Split(string(line),"=");
		if len(str)!=2 {
			continue
		}
		strings.Trim(str[0]," ")
		strings.Trim(str[1]," ")
		configMap[str[0]]=str[1]
	}
	fmt.Println(configMap)
}*/
